package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;//图片表对应的类；

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;//公寓杂费用对应表

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;//公寓配套对应的表

    @Autowired
    private ApartmentLabelService apartmentLabelService;//公寓标签对应的表

    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        //先将apartmentSubmitVo里面的基础值进行赋值
        saveOrUpdate(apartmentSubmitVo);//会自动回显id值，所以要先一步进行判断或者将id提出来
//写自己的逻辑
        //因为自定义保存或者更新，所以判断有无id值来进行不同操作
        if (id != null) {//有值，进行修改,所以要将本id所对应的值全部逻辑删除

            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            queryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(queryWrapper);

            LambdaQueryWrapper<ApartmentFeeValue> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(queryWrapper1);

            LambdaQueryWrapper<ApartmentFacility> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(queryWrapper2);

            LambdaQueryWrapper<ApartmentLabel> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(queryWrapper3);
        }

        //删除完后进行添加
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfoService.saveOrUpdate(graphInfo);
            }
        }

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId).build();
                apartmentFeeValueService.saveOrUpdate(build);
            }
        }

        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                apartmentFacilityService.saveOrUpdate(build);
            }
        }

        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            for (Long labelId : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId).build();
                apartmentLabelService.saveOrUpdate(build);
            }
        }
    }

    //分页查询
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    //根据id获得公寓信息
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {

        //查询apartmentInfo查询出来有可能为空//错误id
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo==null)
            return null;
        //查询graphInfo，因为前端只接受图片名字和图片地址，所以用vo来接收信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        //查询labelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        //5.查询FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;
    }

    //根据id删除公寓信息
    @Override
    public void removeApartmentById(Long id) {
        //先判断该公寓是否有对应的房间信息，如果有的话就不删除，返回特定状态码
        LambdaQueryWrapper<RoomInfo> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(RoomInfo::getApartmentId,id);
        Long aLong = roomInfoMapper.selectCount(queryWrapper4);
        if (aLong>0){
            //进入if代表它有值，不能进行删除，返回特定的返回值
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //删除公寓信息（逻辑删除）
        this.removeById(id);


        //防止孤儿信息，删除相关信息

        //1删除GraphInfo
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
        graphInfoService.remove(queryWrapper);

        //2删除ApartmentLabel（标签）
        LambdaQueryWrapper<ApartmentLabel> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(queryWrapper1);

        //3删除ApartmentFacility(配套)
        LambdaQueryWrapper<ApartmentFacility> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(queryWrapper2);

        //4删除ApartmentFeeValue(杂费)
        LambdaQueryWrapper<ApartmentFeeValue> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(queryWrapper3);
    }
}




